import React, { useEffect, useState } from "react";
import './index.css'
import Heand from "../component/heand";
import Sp from "../component/zhanshi";
import get from "../api/api";
import { useNavigate } from "react-router-dom";
import { isLogined } from "./auth";
import { useParams } from "react-router-dom"
import axios from "axios";


function Mygwc(props) {
    //  const history = useHistory();

    const navigate = useNavigate()

    const row = []
    const [listt, setListt] = useState(null)
    const gly=-1
    const goxq = (objectid) => {
        navigate(`/${objectid}`,{state:{gly}})
    }
    const people_id= sessionStorage.getItem('username')
    useEffect(() => {
            if(isLogined()===false){

                    navigate('/login')


        }
        get(`https://r5ctn9z9.lc-cn-n1-shared.com/1.1/classes/food?where={"people_id":"${people_id}"}`, {
            " X-LC-Id": "r5CTN9z96pCW8mI8fx9YxOmw-gzGzoHsz",
            "X-LC-Key": "8gSsl4Sz0ILMiQeMNMA8xQqg",
            "Content-Type": ""
        })
            .then((res) => {
                setListt(res.results
                )

            })
            .catch((error) => {
                console.log(error);
            });
    }, []);
    // const { objectId } = useParams();
    
   

    (listt || []).forEach((item) => {
        row.push(

            
                <li className="myhomeli" onClick={() => goxq(item.Commodity_id)} key={item.objectId}>
                <Sp className="image" image={item.image} key={item.objectId} xiangqing={item.xiangqing} price={item.price} />
                
            </li>
            
            

        )
    });
    // console.log(row)
    // console.log(isLogined())

    return (isLogined()?

  

            <div>
                <Heand />

                <div className="myhome">
                    <ul>

                        {row}


                    </ul>



                </div>

            </div> :navigate("/login")


    )
}
export default Mygwc